﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.SeamCarving
{
    /// <summary>
    /// Seam class
    /// </summary>
    public class Seam
    {
        public int Pixel = 0; // Which pixel (x or y) does the seam start at?
        public int TotalDiff = 0; // Total of energy *differences* across seam
        public int TotalEnergy = 0; // Total energy of all pixels in seam
        public int FromPixel = 0; // Leftmost/topmost pixel offset position that fully encompasses this seam
        public int ToPixel = 0; // Rightmost/bottommost pixel offset position that fully encompasses this seam
        public double CompareValue = 0; // Value to compare seams against if determining lowest energy (accomodates functions)
        // Direction of the seam
        public Direction Direction { get; internal set; }
        // Data for each pixel in the seam
        public SeamPixel[] SeamPixels { get; internal set; }

        public IEnumerable<Point> SeamPoints(Size size)
        {
            int x = 0, y = 0;
            int xInc = 0, yInc = 0; // x and y increments
            int pixelCount; // Maximum number of pixels
            int pixelIndex = 0;

            // Set initial values
            if (Direction == Direction.Vertical)
            {
                x = Pixel;
                yInc = 1;
                pixelCount = size.Height;
            }
            else
            {
                y = Pixel;
                xInc = 1;
                pixelCount = size.Width;
            }

            // Iterate through seam
            while (x < size.Width && y < size.Height)
            {
                SeamPixel seamPixel = SeamPixels[pixelIndex];
                pixelIndex++;
                if (seamPixel.Left)
                    if (Direction == Direction.Vertical) x++;
                    else y--;
                else if (seamPixel.Right)
                    if (Direction == Direction.Vertical) x--;
                    else y++;
                yield return new Point(x, y);

                // Next pixel
                x += xInc;
                y += yInc;
            }
        }
    }
}
